package mt.server;

public class Link {

	public int serverOrderID;
	public String nickname;
	public String stock;
	public boolean isBuyOrder;
	public int numberOfUnits;
	public double pricePerUnit;

	public Link next;

	public Link(int serverOrderID, String nickname, String stock,
			boolean isBuyOrder, int numberOfUnits, double pricePerUnit) {

		this.serverOrderID = serverOrderID;
		this.nickname = nickname;
		this.stock = stock;
		this.isBuyOrder = isBuyOrder;
		this.numberOfUnits = numberOfUnits;
		this.pricePerUnit = pricePerUnit;

	}

	public void display() {

		System.out.println(serverOrderID + ":" + nickname + ":" + stock + ":"
				+ isBuyOrder + ":" + numberOfUnits + ":" + pricePerUnit);

	}

	public String tostring() {

		return nickname;

	}

	public static void main(String[] args) {

		linklist newlinklist = new linklist();
		newlinklist.insertLink(1, "Bruno", "BES", true, 200, 50);
		newlinklist.insertLink(2, "Nuno", "BES2", true, 300, 150);
		newlinklist.insertLink(3, "Aline", "BES3", true, 400, 250);
		newlinklist.insertLink(4, "Edy", "BES4", true, 500, 350);
		newlinklist.insertLink(5, "David", "BES5", false, 500, 350);
		newlinklist.insertLink(6, "Jay", "BES6", false, 500, 350);

		newlinklist.display();

		try {

			System.out.println(newlinklist.find("BES5", false).nickname+ " Was Found");

		} catch (Exception e) {

			System.out.println("The order wasn't Found");

		}

		newlinklist.removeLink("BES6", false);

		newlinklist.display();

	}

}

class linklist {

	public Link firstlink;

	linklist() {

		firstlink = null;

	}

	public boolean isEmpty() {

		return (firstlink == null);

	}

	public void insertLink(int serverOrderID, String nickname, String stock,
			boolean isBuyOrder, int numberOfUnits, double pricePerUnit) {

		Link newlink = new Link(serverOrderID, nickname, stock, isBuyOrder,
				numberOfUnits, pricePerUnit);

		newlink.next = firstlink;

		firstlink = newlink;

	}

	public void display() {

		Link theLink = firstlink;

		while (theLink != null) {

			theLink.display();

			System.out.println("Next Link: " + theLink.next);

			theLink = theLink.next;

			System.out.println();

		}

	}

	public Link find(String stoc, boolean isBuyOrder) {

		Link theLink = firstlink;

		if (!isEmpty()) {

			while (theLink.stock != stoc || theLink.isBuyOrder != isBuyOrder) {

				if (theLink.next == null) {

					return null;

				} else {

					theLink = theLink.next;

				}

			}

		} else {

			System.out.println("Empty LinkedList");

		}

		return theLink;

	}

	public Link removeLink(String stoc, boolean isBuyOrder) {

		Link currentLink = firstlink;
		Link previousLink = firstlink;

		while (currentLink.stock != stoc || currentLink.isBuyOrder != isBuyOrder) {

			if (currentLink.next == null) {

				return null;

			} else {

				previousLink = currentLink;
				currentLink = currentLink.next;

			}

		}

		if (currentLink.stock == firstlink.stock) {

			firstlink = firstlink.next;

		} else {

			System.out.println("FOUND A MATCH");
			System.out.println("currentLink: " + currentLink);
			System.out.println("firstLink: " + firstlink);

			previousLink.next = currentLink.next;

		}

		return currentLink;

	}

	public Link send() {

		Link theLink = firstlink;

		return theLink;

	}

}